package com.dys.testarray;

import java.util.Arrays;

/**
 * @className: com.dys.testarray.ArraysTest
 * @description: Arrays工具类的使用
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/07/13 下午9:14
 */
public class ArraysTest {

    public static void main(String[] args) {
        fillArray();
    }

    public static void testArrays() {
        int[] arr = {1, 3, 11, 33, 77, 88, 100};
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i != arr.length - 1) {
                System.out.print(arr[i] + ", ");
            } else {
                System.out.print(arr[i] + "]");
            }
        }
    }

    /**
     * 将 int数组 转为 String
     *
     * @param arr 等待转换的数组
     * @return
     */
    public static String intArrayToString(int[] arr) {
        if (arr == null) {
            return "null";
        }
        int iMax = arr.length - 1;
        if (iMax == -1) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; ; i++) {
            sb.append(arr[i]);
            if (i == iMax) {
                return sb.append("]").toString();
            }
            sb.append(", ");
        }
    }

    public static void testArrays3() {
        int[] arr = {1, 3, 11, 33, 77, 88, 100};
        String array = intArrayToString(arr);
        System.out.println(array);
    }

    /**
     * 遍历查看数组元素
     */
    public static void showArray() {
        int[] arr = {1, 3, 11, 33, 77, 88, 100};
        String string = Arrays.toString(arr);
        System.out.println(string);
    }

    /**
     * 查找指定数组中指定元素对应的指定索引
     * binarySearch: 二分法查找的前提是一个有序的数组
     */
    public static void findElement() {
        int[] arr = {1, 3, 55, 33, 77, 88, 100};
        Arrays.sort(arr);
        System.out.println("排序之后的数组; " + Arrays.toString(arr));
        int index = Arrays.binarySearch(arr, 3);
        System.out.println(index);
    }

    /**
     * 复制数组
     */
    public static void copyArray() {
        int[] arr = {1, 3, 55, 33, 77, 88, 100};
        // copyOf: 完成数组的复制
        int[] newArray = Arrays.copyOf(arr, arr.length);
        System.out.println("复制之后的数组; " + Arrays.toString(newArray));
    }

    /**
     * 复制数组,区间复制
     */
    public static void copyArrayOfRange() {
        int[] arr = {1, 3, 55, 33, 77, 88, 100};
        // copyOfRange: 区间复制
        int[] newArray = Arrays.copyOfRange(arr, 1, 4);// [1,4)
        System.out.println("区间复制之后的数组; " + Arrays.toString(newArray));
    }

    /**
     * 比较2个数组中的元素是否相等
     */
    public static void equalsArray() {
        int[] arr = {1, 3, 55, 33, 77, 88, 100};
        int[] arr2 = {1, 3, 55, 33, 77, 88, 100};
        boolean b = Arrays.equals(arr, arr2);
        System.out.println(b);
        // 比较的是两侧的地址值，返回结果一定是false
        System.out.println(arr == arr2);
    }

    /**
     * 填充数组
     */
    public static void fillArray() {
        int[] arr = {1, 3, 55, 33, 77, 88, 100};
        Arrays.fill(arr, 10);
        System.out.println(Arrays.toString(arr));
    }
}
